package utility;



import implementor.BuildingImpl;
import implementor.CategoryMasterImplementor;
import implementor.ContractorImpl;
import implementor.EmployeeImpl;
import implementor.FlatImpl;
import implementor.MaterialMasterImplementor;
import implementor.MaterialSpecificationImplementor;
import implementor.ProjectImpl;
import implementor.PurchaseOrderImpl;

import java.util.HashMap;

import javax.swing.JComboBox;

import model.CategoryMaster;

import dao.BuildingDao;
import dao.CategoryMasterDao;
import dao.ContractorDao;
import dao.EmployeeDao;
import dao.FlatDao;
import dao.MaterialMasterDao;
import dao.MaterialSpecificationDao;
import dao.ProjectDao;
import dao.PurchaseOrderDao;
import dto.Building;
import dto.Contractor;
import dto.Employee;
import dto.Flat;
import dto.MaterialMaster;
import dto.MaterialSpecification;
import dto.Project;
import dto.PurchaseOrder;

public class AppUtilities {
	private static HashMap<String, String> categories;
	private static HashMap<String, Integer> generation;
	private static HashMap<String, Integer> project;
	private static HashMap<String, Integer> contractors;
	private static HashMap<String, Integer> bldgs;
	private static HashMap<String, Integer> flats;
	private static HashMap<String, String> materials;
	
	
	
	public static void InitAllHashMaps()
	{  
		categories = new HashMap<String, String>();
		generation = new HashMap<String, Integer>();
		contractors=new HashMap<String, Integer>();
		bldgs=new HashMap<String, Integer>();
		flats=new HashMap<String, Integer>();
		project = new HashMap<String, Integer>();
		//materials=new HashMap<String, String>();
		CategoryMasterDao dao=new CategoryMasterImplementor();
		try {
			CategoryMaster[] catMaster=dao.retreiveAll();
			for(CategoryMaster temp:catMaster)
			   categories.put(temp.getCatName(),temp.getCatCode());
		
		
			EmployeeDao dao1=new EmployeeImpl();
			Employee[] employees=dao1.retreiveAll();
			for(Employee temp:employees)
			   generation.put(temp.getEmployeeName(),temp.getAutoEmployeeId());
		
			ContractorDao cDao=new ContractorImpl();
				Contractor[] c=cDao.retreiveAll();
				for(Contractor temp:c)
					contractors.put(temp.getContractorName(),temp.getAutoContractorId());
				
				BuildingDao bDao=new BuildingImpl();
					Building[] c1=bDao.retreiveAll();
					for(Building temp:c1)
						bldgs.put(temp.getBuildingName(),temp.getAutoIdBuilding());


					FlatDao fDao=new FlatImpl();
						Flat[] flat=fDao.retreiveAll();
						for(Flat temp:flat)
							flats.put(temp.getFlatName(),temp.getAutoIdFlat());

						ProjectDao pdao=new ProjectImpl();
							Project[] proj=pdao.retreiveAll();
							for(Project temp:proj)
							   project.put(temp.getProjectName(),temp.getAutoIdProject());
		
		} catch (Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
	public static String getCategoryName(String catName )
	{
		if(categories==null)
		{
			categories = new HashMap<String, String>();
			CategoryMasterDao dao=new CategoryMasterImplementor();
			try {
				CategoryMaster[] catMaster=dao.retreiveAll();
				for(CategoryMaster temp:catMaster)
				   categories.put(temp.getCatName(),temp.getCatCode());
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return categories.get(catName);
	}
	
	public static int getGeneratedById(String generatedBy)
	{
		if(generation==null)
		{
			generation = new HashMap<String, Integer>();
			EmployeeDao dao=new EmployeeImpl();
			try {
				Employee[] employees=dao.retreiveAll();
				for(Employee temp:employees)
				   generation.put(temp.getEmployeeName(),temp.getAutoEmployeeId());
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return generation.get(generatedBy);
		
	}
	
	public static String getGenratedByName(int id)
	{
		 for(String s : generation.keySet()){
		        if(generation.get(s).equals(id)) 
		        	return s;
		    }
		 return null;
	
	}
	
	
	public static int getContractorId(String contractorname)
	{
		if(contractors==null)
		{
			contractors=new HashMap<String, Integer>();
			ContractorDao cDao=new ContractorImpl();
			try {
				Contractor[] c=cDao.retreiveAll();
				for(Contractor temp:c)
					contractors.put(temp.getContractorName(),temp.getAutoContractorId());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return contractors.get(contractorname);
	}
	
	public static String getContractorName(int id)
	{
		 for(String s : contractors.keySet()){
		        if(contractors.get(s).equals(id)) 
		        	return s;
		    }
		 return null;
	
	}
	
	public static int getBuildingId(String bldgname)
	{
		if(bldgs==null)
		{
			bldgs=new HashMap<String, Integer>();
			BuildingDao bDao=new BuildingImpl();
			try {
				Building[] c=bDao.retreiveAll();
				for(Building temp:c)
					bldgs.put(temp.getBuildingName(),temp.getAutoIdBuilding());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return bldgs.get(bldgname);
	}
	
	public static String getBuildingName(int id)
	{
		 for(String s : bldgs.keySet()){
		        if(bldgs.get(s).equals(id)) 
		        	return s;
		    }
		 return null;
	
	}
	
	public static int getFlatId(String flatname)
	{
		if(flats==null)
		{
			flats=new HashMap<String, Integer>();
			FlatDao fDao=new FlatImpl();
			try {
				Flat[] c=fDao.retreiveAll();
				for(Flat temp:c)
					flats.put(temp.getFlatName(),temp.getAutoIdFlat());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flats.get(flatname);
		
	}
	
	public static String getFlatName(int id)
	{
		 for(String s : flats.keySet()){
		        if(flats.get(s).equals(id)) 
		        	return s;
		    }
		 return null;
	
	}
	public static String getmCode(String materialName)
	{
		if(materials==null)
		{
			materials=new HashMap<String, String>();
			MaterialMasterDao mDao=new MaterialMasterImplementor();
			try {
				MaterialMaster[] c=mDao.retreiveAll();
				for(MaterialMaster temp:c)
					materials.put(temp.getmName(),temp.getmCode());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return materials.get(materialName);
	}
	
	public static int getProjectId(String projectName)
	{
		if(project==null)
		{
			project = new HashMap<String, Integer>();
			ProjectDao dao=new ProjectImpl();
			try {
				Project[] proj=dao.retreiveAll();
				for(Project temp:proj)
				   project.put(temp.getProjectName(),temp.getAutoIdProject());
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return project.get(projectName);
		
	}
	
	public static String getProjectName(int id)
	{
		 for(String s : project.keySet()){
		        if(project.get(s).equals(id)) 
		        	return s;
		    }
		 return null;
	}
	
	public static void fillCategoryCombo(JComboBox combo)
	{
		CategoryMasterDao catDao=new CategoryMasterImplementor();
		CategoryMaster[] c=null;
		try {
			c = catDao.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Category--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getCatName();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);		
	}

	public static void fillProjectNameCombo(JComboBox combo)
	{
		ProjectDao p=new ProjectImpl();
		Project[] project=null;
		try {
			project = p.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[project.length+1];
		items[0]="--Select Project name--";
		for(int i=1;i<=project.length;i++)
		{
			items[i]=project[i-1].getProjectName();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);	
	}
	
	public static void fillPurchaseOrderCombo(JComboBox combo)
	{
		PurchaseOrderDao po=new PurchaseOrderImpl();
		PurchaseOrder[] pon=null;
		try {
			pon = po.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[pon.length+1];
		items[0]="--Select Purchase Order Number--";
		for(int i=1;i<=pon.length;i++)
		{
			items[i]=pon[i-1].getPoNumber();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);	
	}
	
	public static void fillContractorCombo(JComboBox combo)
	{
		ContractorDao cd=new ContractorImpl();
		Contractor[] c=null;
		try {
			c = cd.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Contractor Name--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getContractorName();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);	
	}
	
	public static void fillBuildingCombo(JComboBox combo)
	{
		BuildingDao bd=new BuildingImpl();
		Building[] b=null;
		try {
			b = bd.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[b.length+1];
		items[0]="--Select Building Number--";
		for(int i=1;i<=b.length;i++)
		{
			items[i]=b[i-1].getBuildingName();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);	
	}
	
	public static void fillFlatCombo(JComboBox combo)
	{
		FlatDao fd=new FlatImpl();
		Flat[] f=null;
		try {
			f = fd.retreiveAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[f.length+1];
		items[0]="--Select Purchase Order Number--";
		for(int i=1;i<=f.length;i++)
		{
			items[i]=f[i-1].getFlatName();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);	
	}
	
	public static void fillEmployeeCombo(JComboBox combo)
	{
		EmployeeDao ed=new EmployeeImpl();
		Employee[] emp=null;
		try {
			emp = ed.retreiveAll();
		} catch (Exception e) {
			// TODO Ato-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[emp.length+1];
		items[0]="--Select Employee Name--";
		for(int i=1;i<=emp.length;i++)
		{
			items[i]=emp[i-1].getEmployeeName();
		}
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);	
	}
	
	
	public static void fillSpecificationCombo(JComboBox combo, String matCode)
	{
		MaterialSpecificationDao catDao=new MaterialSpecificationImplementor();
		
		MaterialSpecification[] c=null;
		try {
			c = catDao.retreiveArray("mCode", matCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] items=new String[c.length+1];
		items[0]="--Select Specification--";
		for(int i=1;i<=c.length;i++)
		{
			items[i]=c[i-1].getmSpecs();
		}
		
		ViewUtilities.set_comboBox(combo, items);
		ViewUtilities.setAutoSuggestionsComboBox(combo);		
	}
	
	
	
}
